package com.shpng.cheapenglish.util;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class ParseParameter {

	private static ParseParameter parseParameter;

	private ParseParameter() {

	}

	public static ParseParameter getInstance() {
		if (parseParameter == null)
			parseParameter = new ParseParameter();
		return parseParameter;
	}

	public Object parse(Type type, String[] params) throws ParseException {

		if (type instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) type;
			Class<?> c = (Class<?>) parameterizedType.getRawType();
			if (isList(c)) {
				Type[] types = parameterizedType.getActualTypeArguments();

				if (types[0] instanceof Class) {
					List<Object> list = new ArrayList<Object>();
					for (String param : params) {
						Object object = null;
						object = parse(types[0], new String[] { param });
						list.add(object);
					}
					return list;
				} else {
					return Arrays.asList(params);// ͨ���
				}

			}
		} else if (type instanceof Class) {
			Class<?> clazz = (Class<?>) type;
			if (params.length == 1) {
				if ("java.lang.String".equals(clazz.getName())) {

					return params[0];

				} else if ("java.lang.Integer".equals(clazz.getName())
						|| "int".equals(clazz.getName())) {

					return Integer.parseInt(params[0]);

				} else if ("java.lang.Long".equals(clazz.getName())
						|| "long".equals(clazz.getName())) {

					return Long.parseLong(params[0]);

				} else if ("java.lang.Double".equals(clazz.getName())
						|| "double".equals(clazz.getName())) {

					return Double.parseDouble(params[0]);

				} else if ("java.lang.Boolean".equals(clazz.getName())
						|| "boolean".equals(clazz.getName())) {
					return Boolean.parseBoolean(params[0]);
				} else if ("java.util.Date".equals(clazz.getName())) {

					if (Pattern.matches("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}",
							params[0])) {
						SimpleDateFormat sdf = new SimpleDateFormat(
								"yyyy-MM-dd");
						return sdf.parse(params[0]);

					}
				}// �������飿 �������������֣�
			}
		}

		return null;
	}

	public boolean isList(Class<?> type) {
		if (("java.util.List").equals(type.getName()))
			return true;
		for (Class<?> i : type.getInterfaces()) {
			if (("java.util.List").equals(i))
				return true;
		}
		return false;
	}

	public void test(List<?> type) {

	}

	public static void main(String[] args) throws ParseException {
		ParseParameter p = new ParseParameter();
		System.out.println(p.parse(Integer.class, new String[] { "1" }));
		System.out.println(p.parse(String.class, new String[] { "abc" }));
		for (Method m : ParseParameter.class.getMethods()) {
			if (m.getName().equals("test")) {
				List<?> l = (List<?>) p.parse(m.getGenericParameterTypes()[0],
						new String[] { "10", "22" });
				System.out.println(l.get(1));
			}
		}
	}

}
